package messageMeClient.view;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JLabel;
import javax.swing.JButton;

import messageMeTransfer.connection.ClientConnection;
import messageMeTransfer.packages.CommandPackage;
import messageMeTransfer.utilities.PackageWrapper;
import messageMeUtility.commands.CommandRegister;
import messageMeUtility.user.StandardUser;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RegisterDialog extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldUsername;
	private JTextField textFieldEmail;
	private JPasswordField passwordField;
	private JPasswordField passwordFieldRepeat;
	private JLabel infoLabel;

	private ClientConnection connection = null;
	private PackageWrapper packageWrapper = null;

	public RegisterDialog(ClientConnection connection, PackageWrapper wrapper) {

		this.connection = connection;
		this.packageWrapper = wrapper;

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 263, 208);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		textFieldUsername = new JTextField();
		textFieldUsername.setBounds(145, 11, 86, 20);
		contentPane.add(textFieldUsername);
		textFieldUsername.setColumns(10);

		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(145, 42, 86, 20);
		contentPane.add(textFieldEmail);
		textFieldEmail.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(145, 73, 86, 20);
		contentPane.add(passwordField);

		passwordFieldRepeat = new JPasswordField();
		passwordFieldRepeat.setBounds(145, 104, 86, 20);
		contentPane.add(passwordFieldRepeat);

		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(10, 14, 125, 14);
		contentPane.add(lblUsername);

		JLabel lblEmail = new JLabel("E-Mail");
		lblEmail.setBounds(10, 45, 125, 14);
		contentPane.add(lblEmail);

		JLabel lblPasswod = new JLabel("Password");
		lblPasswod.setBounds(10, 76, 125, 14);
		contentPane.add(lblPasswod);

		JLabel lblPasswordWdh = new JLabel("Password wdh.");
		lblPasswordWdh.setBounds(10, 107, 125, 14);
		contentPane.add(lblPasswordWdh);

		JButton btnRegister = new JButton("Register!");
		btnRegister.addActionListener(this.getRegisterListener());
		btnRegister.setBounds(142, 135, 89, 23);
		contentPane.add(btnRegister);

		infoLabel = new JLabel("Info");
		infoLabel.setBounds(10, 139, 125, 14);
		contentPane.add(infoLabel);
		infoLabel.setVisible(false);
	}

	private ActionListener getRegisterListener() {
		return new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegisterDialog.this.register();
			}
		};
	}
	private void displayInfo(String info) {
		this.infoLabel.setVisible(true);
		this.infoLabel.setText(info);
	}
	private void register() {
		if (this.textFieldUsername.getText().equals("")) {
			this.displayInfo("Invalid Username.");
			return;
		}
		if (this.textFieldEmail.getText().equals("")) {
			this.displayInfo("Invalid E-Mail.");
			return;
		}
		if (this.convertToString(this.passwordField.getPassword()).equals("")) {
			this.displayInfo("Invalid Password.");
			return;
		}
		if (!this.convertToString(this.passwordField.getPassword()).equals(
				this.convertToString(this.passwordFieldRepeat.getPassword()))) {
			this.displayInfo("Password inconsistent.");
			return;
		}
		this.infoLabel.setVisible(false);

		StandardUser user = new StandardUser(this.textFieldUsername.getText(),
				this.textFieldEmail.getText(),
				this.convertToString(this.passwordField.getPassword()));
		CommandRegister cRegister = new CommandRegister(user);
		CommandPackage cPackage = this.packageWrapper.wrapPackage(cRegister);
		this.connection.send(cPackage);
	}
	private String convertToString(char[] charArray) {
		String result = "";

		for (int index = 0; index < charArray.length; index++) {
			result += charArray[index];
		}
		return result;
	}

}
